package cn.ehanghai.iot.back.service;

import cn.ehanghai.iot.back.domain.UserInfo;
import cn.ehanghai.iot.back.mapper.UserInfoMapper;
import cn.ehanghai.iot.common.domain.PageSet;
import cn.ehanghai.iot.config.DefineUtil;
import cn.ehanghai.iot.vo.UserInfoVo;
import cn.ehanghai.iot.vo.UserInviteVo;
import net.ryian.orm.service.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: xumf
 * @Date: 2019/6/26 17:27
 */
@Component
public class UserInfoService extends BaseService<UserInfo,UserInfoMapper> {



    public PageSet selectUserInfoListByPage(Integer pageNum, Integer pageSize,String phone, String status,String province){

        if(pageSize==null){
            pageSize=10;
        }
        if(pageNum==null){
            pageNum=1;
        }

        PageSet page= new PageSet();
        Map<String,Object> map =new HashMap<>();

        map.put("phone",phone);
        map.put("status",status);
        map.put("province",province);
        int count=0;

        count=mapper.getUserInfoCount(map);

        page.setCount(count);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        int start=(pageNum-1)*pageSize;
        int size=pageSize.intValue();
        map.put("start",start);
        map.put("size",size);
        List<UserInfo> list= mapper.getUserInfoList(map);

        List<UserInfoVo> voList=new ArrayList<>();
        for (UserInfo u : list) {
            UserInfoVo userInfoVo=new UserInfoVo();
            BeanUtils.copyProperties(u,userInfoVo);
            voList.add(userInfoVo);
        }
        page.setData(voList);
        return page;
    }

    public PageSet selectinviteListListByPage(Integer pageNum, Integer pageSize,Long userId){

        if(pageSize==null){
            pageSize=10;
        }
        if(pageNum==null){
            pageNum=1;
        }

        PageSet page= new PageSet();
        Map<String,Object> map =new HashMap<>();

        map.put("userId",userId);
        int count=0;

        count=mapper.getUserInfoCount(map);

        page.setCount(count);
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        int start=(pageNum-1)*pageSize;
        int size=pageSize.intValue();
        map.put("start",start);
        map.put("size",size);
        List<UserInfo> list= mapper.getUserInfoList(map);

        List<UserInviteVo> voList=new ArrayList<>();
        for (UserInfo u : list) {
            UserInviteVo inviteVo=new UserInviteVo();
            BeanUtils.copyProperties(u,inviteVo);
            voList.add(inviteVo);
        }
        page.setData(voList);
        return page;
    }

    public  UserInfo getUserInfoByOpenId(String openId){
        return mapper.getByUserInfo(openId);
    }

    public void update(UserInfo userinfo){
        userinfo.setCreateDate(new Date());
        mapper.updateByPrimaryKey(userinfo);
    }

    //用户邀请
    public boolean userInvite(UserInfo userInfo,String fromUserOpenId){
        Boolean flag=fromUserOpenId==null?false:true;
        if(flag&&!userInfo.getOpenId().equals(fromUserOpenId)){
            //被邀请,更新邀请者的信息
            if(userInfo.getInviteId()!=null){
                UserInfo inviteUser= mapper.getByUserInfo(fromUserOpenId);
                if(inviteUser!=null) {
                    userInfo.setInviteId(inviteUser.getId());//设置邀请人Id
                    userInfo.setInviteDate(new Date());
                    inviteUser.setInviteCount(inviteUser.getInviteCount() + 1);
                    mapper.updateByPrimaryKey(inviteUser);
                }
            }
        }
        if(userInfo.getId()==null){
            userInfo.setStatus(DefineUtil.UNCHECK_STATUS);
            userInfo.setFollow(DefineUtil.UNFOLLOW_SATATUS);
            userInfo.setInviteCount(0);
            userInfo.setCreateDate(new Date());
            mapper.insert(userInfo);
        }
        else{
            mapper.updateByPrimaryKey(userInfo);
        }
        return true;


    }

}
